package smartcampus.scjava.controller.forth;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import smartcampus.scjava.POJO.DO.Equipment;
import smartcampus.scjava.POJO.DO.EquipmentDamageReport;
import smartcampus.scjava.POJO.DO.EquipmentInventoryRecord;
import smartcampus.scjava.custom.ResultVO;
import smartcampus.scjava.service.IEquipmentDamageReportService;
import smartcampus.scjava.service.IEquipmentInventoryRecordService;
import smartcampus.scjava.service.IEquipmentService;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 器材损坏报备表 前端控制器
 * </p>
 *
 * @author xian
 * @since 2023-12-05
 */
@RestController
@RequestMapping("/equipment-damage-report")
public class EquipmentDamageReportController {
    @Resource
    private IEquipmentDamageReportService damageReportService;

    @Resource
    private IEquipmentService equipmentService;


    @RequestMapping("/listPage")
    public ResultVO<?> listPageBook(Integer  pageNum, Integer pageSize, String word) {
        if (pageNum == null || pageSize == null) {
            return ResultVO.error("分页参数错误");
        }

        Page<EquipmentDamageReport> page = new Page<>(pageNum,pageSize);
        QueryWrapper<EquipmentDamageReport> query = new QueryWrapper<>();
        if (StringUtils.hasText(word)) {
            query.lambda().like(EquipmentDamageReport::getContent,word).
                    or().like(EquipmentDamageReport::getEquipmentNo,word);
        }


        damageReportService.page(page,query);

        return ResultVO.success(page);
    }

    @RequestMapping("/add")
    @Transactional(rollbackFor = Exception.class)
    public ResultVO<?> add(@RequestBody EquipmentDamageReport equipment) {


        Equipment byId = equipmentService.getById(equipment.getEquipmentId());
        if (byId == null) {
            return ResultVO.error("设备不存在！");
        }
        // 判断是否有存在待修理的记录
        QueryWrapper<EquipmentDamageReport> query = new QueryWrapper<>();
        query.lambda().eq(EquipmentDamageReport::getEquipmentId,equipment.getEquipmentId())
                .ne(EquipmentDamageReport::getDamageStatus,3);
        long count = damageReportService.count(query);
        if (count > 0) {
            return ResultVO.error("该设备存在待修理记录，请处理后再操作！");
        }
        // 更新器材状态
        equipment.setCreateTime(LocalDateTime.now());
        equipment.setUpdateTime(LocalDateTime.now());
        equipment.setDamageStatus(0);
        damageReportService.save(equipment);
        equipmentService.updateById(byId.setEquipmentStatus(0));
        return ResultVO.success("添加成功！");
    }

    @RequestMapping("/delete")
    public ResultVO<?> delete(Integer damageId) {

        EquipmentDamageReport byId = damageReportService.getById(damageId);
        if (byId == null) {
            return ResultVO.error("删除失败！");
        }
        damageReportService.removeById(damageId);
        // 修改器材状态为正常
        UpdateWrapper<Equipment> uw = new UpdateWrapper<>();
        uw.lambda().set(Equipment::getEquipmentStatus,1).eq(Equipment::getId,byId.getEquipmentId());
        equipmentService.update(uw);
        return ResultVO.success("删除成功！");
    }

    @RequestMapping("/update")
    @Transactional(rollbackFor = Exception.class)
    public ResultVO<?> update(@RequestBody EquipmentDamageReport damageReport) {

        EquipmentDamageReport byId = damageReportService.getById(damageReport.getId());
        if (byId == null) {
            return ResultVO.error("记录不存在！");
        }
        damageReportService.updateById(damageReport);
        if (damageReport.getDamageStatus() == 3) {
            // 器材状态修改为正常
            equipmentService.updateById(new Equipment().setId(byId.getEquipmentId()).setEquipmentStatus(1));
        }
        return ResultVO.success("更新成功！");
    }
}
